<template>
	<view class="content">
		<uv-navbar title="" :placeholder="true" @leftClick="leftClick"></uv-navbar>
		<view class="content_box">
			<view class="content_box_title">
				密码登录
			</view>
			<view class="content_box_input">
				<view class="name">账号</view>
				<input type="text" v-model="user" placeholder="请输入账号"/>
			</view>
			<view class="content_box_input">
				<view class="name">密码</view>
				<input type="text" :password="true" v-model="pass" placeholder="请输入密码"/>
			</view>
			<view class="content_box_text">
				<text @click="leftClick">短信登录</text>
				<text @click="link('/user/login/retrieve')">忘记密码</text>
			</view>
			<view class="content_box_button" @click="denglu">
				登录
			</view>
			<view @click="link('/user/login/register')" class="content_box_zc">注册新账号</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		homeApi
	} from "@/api/index"
	import {link} from "@/utils/utils.js"
	const user = ref('')
	const pass = ref('')
	const userInfo = ref({})
	const denglu = async()=>{
		let data = {
			user:user.value,
			pass:pass.value,
		}
		let res = await homeApi.publicPost(data,'userlogin')
		if (res.error_no == '0') {
			for (let i = 0; i < res.columns.length; i++) {
				if (res.columns[i].name == 'userinfo') {
					userInfo.value = res.columns[i]
				}
			}
			uni.setStorageSync('token', res.token)
			if(userInfo.value.openid=='ok'){
				uni.showToast({
					title: '登录成功！',
					icon: 'success',
					duration: 2000
				});
			}
			isUserCode()
		} else {
			uni.showToast({
				title: res.error_msg,
				icon: 'success',
				duration: 2000
			});
		}
	}
	const isUserCode = () => {
		uni.login({
			provider: 'weixin', //使用微信登录
			success: function(loginRes) {
				wxlogin(loginRes.code);
			}
		})
	}
	const wxlogin = async (code) => {
		let data = {
			do: 'wxcodelogin',
			code: code,
		}
		let res = await homeApi.publicGet(data)
		uni.showToast({
			title: '登录成功！',
			icon: 'success',
			duration: 2000
		});
		setTimeout(() => {
			uni.navigateBack({
				delta:3
			})
		}, 2000)
	}
	
	const leftClick = () => {
		uni.navigateBack({
			delta: 1
		})
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	
		&_box {
			width: 100%;
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			&_title{
				color: #e94f06;
				font-size:42rpx;
				font-weight: bold;
				margin-top: 5vh;
				padding-bottom: 7vh;
			}
			&_input{
				display: flex;
				align-items: center;
				width: 90%;
				border-bottom: 1px solid #dededf;
				height: 90rpx;
				margin-top: 30rpx;
				.name{
					font-size: 32rpx;
					width: 190rpx;
				}
				input{
					flex: 1;
					height: 90rpx;
					font-size: 30rpx;
					margin-right: 20rpx;
				}
				.getyzm{
					padding: 10rpx 20rpx;
					border: 1px solid #e94f06;
					border-radius: 30px;
					font-size: 30rpx;
					color: #e94f06;
				}
				.yzm{
					width: 200rpx;
				}
			}
			&_text{
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 30rpx;
				color: #e94f06;
				margin-top: 20rpx;
			}
			&_button{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				background: #e94f06;
				margin-top: 60rpx;
				font-size: 36rpx;
				border-radius: 30px;
			}
			&_zc{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
			}
		}
	}
</style>